import { _decorator, Component, Node,color,Sprite,tween,Vec3, v3,Quat, quat} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('StarAnim')
export class StarAnim extends Component {

    private sprite:Sprite=null;

    private a:number=0;
    private playAnim:boolean=false;

    private r:Quat=quat(0,0,0,0);

    private static readonly scale1:Vec3=v3(0.3, 0.3, 1);
    private static readonly scale2:Vec3=v3(0.5, 0.5, 1);

    onLoad()
    {
        this.sprite=this.node.getComponent(Sprite);
        this.sprite.color=color(255, 234, 168, 0);
        this.node.setScale(0.3, 0.3, 1);

        Quat.fromEuler(this.r, 0, 0,350);
    }

    start()
    {
        this.scheduleOnce(()=>{
            this.play();
        }, 2);
    }

    public play()
    {
        this.playAnim=true;
        this.a=0;
    }

    update(deltaTime: number)
    {
        if(!this.playAnim)
            return;
        if(this.a<255)
            this.a+=7;
        if(this.a>255)
        {
            this.a = 255;
            tween(this.node).to(1.2, { scale: StarAnim.scale2}).to(1.2, { scale: StarAnim.scale1}).union().repeatForever().start();
        }

        if(this.a<255)
            this.sprite.color=color(255, 234, 168, this.a);
    }
}

